package com.itheima.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import org.springframework.util.ResourceUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * @author wmh
 * @version 1.0 2022/6/3
 * @Description: excel模板导出引擎
 **/
public class ExcelExportEngine {
    private static SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");


    public static Workbook writeToExcel(Object object,Workbook workbook,String photoPath) throws Exception {
        //把bean转成map
        Map<String, Object> map = EntityUtils.entityToMap(object);
        Sheet sheet = workbook.getSheetAt(0);
        Row row = null;
        Cell cell = null;
        //循环100行，每一行循环100个单元格---定位到具体的cell
        for (int i = 0; i < 10; i++) {
            row = sheet.getRow(i);
            if(row == null){
                break;}
            else {
                for (int j = 0; j < 10; j++) {
                    cell = row.getCell(j);
                    if(cell != null){
                        writeToCell(cell,map);
                    }
                }
            }
        }

        //处理图片
        if(StringUtils.isNotBlank(photoPath)){
            //File rootPath = new File(ResourceUtils.getURL("classpath:").getPath()); //SpringBoot项目获取根目录的方式
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
//        BufferedImage是一个带缓冲区图像类,主要作用是将一幅图片加载到内存中
            BufferedImage bufferImg = ImageIO
                    .read(new File(photoPath));
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
            Drawing patriarch = sheet.createDrawingPatriarch();
            //获取模板图片位置
            Sheet sheet2 = workbook.getSheetAt(1);
            row = sheet2.getRow(0);
            int col1 = ((Double) row.getCell(0).getNumericCellValue()).intValue();
            int row1 = ((Double) row.getCell(1).getNumericCellValue()).intValue();
            int col2 = ((Double) row.getCell(2).getNumericCellValue()).intValue();
            int row2 = ((Double) row.getCell(3).getNumericCellValue()).intValue();
//            锚点，固定点
            ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0,  col1, row1, col2, row2);
            patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), Workbook.PICTURE_TYPE_JPEG));
            //删除第二个sheet
            workbook.removeSheetAt(1);
        }

        return workbook;

    }

    //比较单元格中的值，是否和map中的key一致，如果一致向单元格中放入map这个key对应的值
    private static void writeToCell(Cell cell, Map<String, Object> map) {
        CellType cellType = cell.getCellType();
        //公式的话不处理
        switch(cellType){
            case FORMULA:{
                break;
            } default:{
                String cellValue = cell.getStringCellValue();
                if(StringUtils.isNotBlank(cellValue)){
                   // System.out.println("获取到的cellValue"+cellValue);
                    for (String key : map.keySet()){
                        if(key.equals(cellValue)){
                            cell.setCellValue(map.get(key).toString());
                     //       System.out.println("获取到的value:"+map.get(key).toString());
                        }
                    }
                }
            }
        }
    }
}